﻿using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Rendering;
using UnityEngine.UI;

namespace Framework.Utils.UITools
{
    [AddComponentMenu("UI/RaycastMask")]
    [DisallowMultipleComponent]
    [RequireComponent(typeof(RectTransform))]
    [RequireComponent(typeof(CanvasRenderer))]
    public class RaycastMask : Graphic
    {
        [SerializeField] private Color _color = new Color(0.51f, 0f, 0f, 0.67f);

        public override Color color
        {
            get { return _color; }
            set
            {
                if (SetColor(ref this._color, value))
                {
#if UNITY_EDITOR
                    if (this.IsDebug)
                        this.SetVerticesDirty();
#endif
                }
            }
        }

#if UNITY_EDITOR
        [SerializeField] private bool IsDebug;
#endif

        protected override void UpdateGeometry()
        {
#if UNITY_EDITOR
            base.UpdateGeometry();
#endif
        }

        public override void SetVerticesDirty()
        {
#if UNITY_EDITOR
            base.SetVerticesDirty();
#endif
        }

        protected override void OnPopulateMesh(VertexHelper vh)
        {
#if UNITY_EDITOR
            if (this.IsDebug)
            {
                base.OnPopulateMesh(vh);
            }
            else
            {
                vh.Clear();
            }
#endif
        }

        public static bool SetColor(ref Color currentValue, Color newValue)
        {
            if (currentValue.r == newValue.r && currentValue.g == newValue.g && currentValue.b == newValue.b &&
                currentValue.a == newValue.a)
                return false;

            currentValue = newValue;
            return true;
        }
    }
}